<template>
	<view class="uni-numbox" :class="{'booking-style':bookingStyle, 'orange-style':orangeStyle, 'groups-tyle':groupsStyle}">
		<view @click="_calcValue('minus')" class="uni-numbox__minus" :class="{ 'disabled': inputValue <= min || disabled }">
			<text class="uni-numbox--text iconfont" :class="{ 'uni-numbox--disabled': inputValue <= min || disabled }">&#xe64a;</text>
		</view>
		<input :disabled="disabled || inputDisabled" @blur="_onBlur" class="uni-numbox__value" type="number" v-model="inputValue" />
		<view @click="_calcValue('plus')" class="uni-numbox__plus" :class="{ 'disabled': inputValue >= max || disabled }">
			<text class="uni-numbox--text iconfont" :class="{ 'uni-numbox--disabled': inputValue >= max || disabled }">&#xe648;</text>
		</view>
	</view>
</template>
<script>
	/**
	 * 数字输入框
	 * @property {Boolean} bookingStyle 绿色按钮样式（预订使用）
	 * @property {Boolean} groupsStyle  紫色按钮样式（拼团使用）
	 */
	export default {
		name: "UniNumberBox",
		props: {
			value: {
				type: [Number, String],
				default: 1
			},
			min: {
				type: [Number, String],
				default: 0
			},
			max: {
				type: [Number, String],
				default: 100
			},
			step: {
				type: Number,
				default: 1
			},
			disabled: {
				type: Boolean,
				default: false
			},
			inputDisabled:{
				type: Boolean,
				default: false
			},
			bookingStyle: {
				type: Boolean,
				default: false
			},
			orangeStyle: {
				type: Boolean,
				default: false
			},
			groupsStyle: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				inputValue: 0
			};
		},
		watch: {
			value(val) {
				this.inputValue = +val;
			},
			
			inputValue(newVal, oldVal) {
				
				if (+newVal !== +oldVal) {
					this.$emit("change", newVal);
				}
			}
		},
		created() {
			this.inputValue = +this.value;
		},
		methods: {
			_calcValue(type) {
				if (this.disabled) {
					return;
				}
				const scale = this._getDecimalScale();
				let value = this.inputValue * scale;
				let step = this.step * scale;
				if (type === "minus") {
					value -= step;
					if (value < this.min) {
						return;
					}
					if(value > this.max){
						value = this.max
					}
				} else if (type === "plus") {
					value += step;
					if (value > this.max) {
						this.$emit('maxCallback')
						return;
					}
					if(value < this.min){
						value = this.min
					}
				}

				this.inputValue = String(value / scale);
			},
			_getDecimalScale() {
				let scale = 1;
				// 浮点型
				if (~~this.step !== this.step) {
					scale = Math.pow(10, (this.step + "").split(".")[1].length);
				}
				return scale;
			},
			_onBlur(event) {
				let value = event.detail.value;
				if (!value) {
					// this.inputValue = 0;
					return;
				}
				value = +value;
				if (value > this.max) {
					value = this.max;
				} else if (value < this.min) {
					value = this.min;
				}
				this.inputValue = value;
			}
		}
	};
</script>
<style lang="scss" scoped>
	$box-height: 35px;
	/* #ifdef APP-NVUE */
	$box-line-height: 35px;
	/* #endif */
	$box-line-height: 26px;
	$box-width: 35px;

	.uni-numbox {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		height: $box-height;
		line-height: $box-height;
		width: 120px;
	}

	.uni-numbox__value {
		// background-color: $uni-bg-color;
		width: 54rpx;
		height: 23px;
		min-height: 21px !important;
		text-align: center;
		font-size: $uni-font-size-lg;
		// border-width: 1rpx;
		// border-style: solid;
		// border-color: #FFFFFF;
		// border-left-width: 0;
		// border-right-width: 0;
	}

	.uni-numbox__minus {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		justify-content: center;
		width: $box-width;
		height: $box-height;
		// line-height: $box-line-height;
		// text-align: center;
		font-size: 20px;
		color: $uni-text-color;
		// background-color: $uni-bg-color-grey;
		// border-width: 1rpx;
		// border-style: solid;
		// border-color: $uni-border-color;
		// border-top-left-radius: $uni-border-radius-base;
		// border-bottom-left-radius: $uni-border-radius-base;
		// border-right-width: 0;
		border-radius: 52rpx;
		background-color: #f9f9f9;
		border: 1px solid #e5e5e5;
	}

	.uni-numbox__plus {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		align-items: center;
		justify-content: center;
		width: $box-width;
		height: $box-height;
		// border-width: 1rpx;
		// border-style: solid;
		// border-color: $uni-border-color;
		// border-top-right-radius: $uni-border-radius-base;
		// border-bottom-right-radius: $uni-border-radius-base;
		// background-color: $uni-bg-color-grey;
		// border-left-width: 0;
		border-radius: 52rpx;
		background-color: #f9f9f9;
		border: 1px solid #e5e5e5;
	}

	.uni-numbox--text {
		font-size: 26rpx;
		// color: $uni-text-color;
	}

	.uni-numbox--disabled {
		// color: $uni-text-color-disable;
		color: #dcdcdc!important;
	}
	.booking-style,.orange-style,.groups-tyle{
		width: auto;
		height: auto;
		.uni-numbox__minus,.uni-numbox__plus{
			height: 48rpx;
		}
		.uni-numbox__value{
			height: 52rpx;
		}
		.uni-numbox__minus,.uni-numbox__plus{
			width: 48rpx;
			background-color: #f9f9f9;
			border-color: #e5e5e5;
			&.disabled{
				// background-color: $uni-text-color-disable;
				// border-color: $uni-text-color-disable;
				background: #F9F9F9;
				border: 2rpx solid #E5E5E5;
				
			}
		}
		.uni-numbox__value{
			font-size: 28rpx;
			// border-color: #f0f0f0;
			// background-color: #f0f0f0;
		}
		.uni-numbox--text{
			color: #999;
		}
	}
	.booking-style{
		.uni-numbox__minus,.uni-numbox__plus{
			background-color: #f9f9f9;
			border-color: #e5e5e5;
		}
	}
	.orange-style{
		.uni-numbox__minus,.uni-numbox__plus{
			background-color: #ff9000;
			border-color: #ff9000;
		}
	}
	.groups-tyle{
		.uni-numbox__minus,.uni-numbox__plus{
			background-color: #fd2963;
			border-color: #fd2963;
		}
	}
</style>
